package org.ph.share.lock.test.thread2;

import lombok.extern.slf4j.Slf4j;

/**
 * 实现俩个线程，先打印2再打印1
 * 方法1：synchronized关键字、wait()、notify() {@link Thread12}
 * 方法2：ReentrantLock、await、signal {@link Thread13}
 * 方法3：park与unpark (通行证策略){@link Thread14}
 * 方法1与方法2相似
 *
 * @author Hush.
 * @since 2023/5/18 12:53
 */
@Slf4j
public class Thread12 {
    static final Object lock = new Object();

    static boolean FLAG = false;
    public static void main(String[] args) {
        new Thread(() -> {
            synchronized (lock) {
                while (!FLAG) {
                    try {
                        lock.wait();
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }
                log.info("1");
            }
        }, "t-1").start();

        new Thread(() -> {
            synchronized (lock) {
                FLAG = true;
                lock.notify();
                log.info("2");
            }
        }, "t2").start();

    }
}
